package com.rimi.controller;

import com.rimi.bean.YqzcGoods;
import com.rimi.commons.CommonResult;
import com.rimi.model.dto.GoodsListDto;
import com.rimi.service.ShoppingTrolleyService;
import com.rimi.util.GetTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luka
 * @version V1.0
 * @date 2021 2021/7/5 0005 14:48
 */
@RestController
@Api(description = "购物车管理",tags = "购物车管理")
@CrossOrigin
public class ShoppingTrolleyController {

    @Autowired
    ShoppingTrolleyService shoppingTrolleyService;

    @GetMapping("/user/shoppingTrolley")
    @ApiOperation("查询购物车商品")
    public CommonResult<List<GoodsListDto>> shoppingTrolley() {
        List<GoodsListDto> allGoodsInShoppingTrolley = shoppingTrolleyService.findAllGoodsInShoppingTrolleyByUserId(GetTokenUtil.getUser().getUserId());
        return CommonResult.onSuccess(allGoodsInShoppingTrolley);
    }

    @PostMapping("/user/addGood")
    @ApiOperation("添加购物车商品")
    public CommonResult addGood(Integer goodId,Integer count) {
        return shoppingTrolleyService.addGoodsToShoppingTrolley(GetTokenUtil.getUser().getUserId(),goodId,count);
    }

    @DeleteMapping("/user/deleteGood")
    @ApiOperation("删除购物车商品")
    public CommonResult deleteGood(Integer goodId) {
        return shoppingTrolleyService.deleteGoodsInShoppingTrolleyByUserId(GetTokenUtil.getUser().getUserId(),goodId);
    }

    @PutMapping("/user/updateGood")
    @ApiOperation("修改购物车商品")
    public CommonResult updateGood(Integer goodId,Integer count) {
        return shoppingTrolleyService.updateGoodsInShoppingTrolleyByUserId(GetTokenUtil.getUser().getUserId(),goodId,count);
    }

}
